<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
 * Model for friends DB table, fields: user_id, friend_id
 *
 * @author Michael Elfimov (www.elfimov.com)
 */

class Friend_Model extends ORM {

	/**
	 * Create row represents two users friendship
	 *
	 * @param integer $userId User ID
	 * @param integer $friendId Friend ID
	 * @return Object Newly created data object
	 */
	public static function create($userId, $friendId) {
		$dbFriend = ORM::factory('friend');

		$dbFriend->user_id = $userId;
		$dbFriend->friend_id = $friendId;

		$dbFriend->save();
		
		return $dbFriend;
	}
	
	/**
	 * Return information about user friends: id, name, profile_url, age
	 *
	 * @param integer $userId User ID
	 * @return array Users list, array of array
	 */
	public static function getFriends($userId) {
		return Database::instance()->query(
			"SELECT id, name, profile_url, age"
			. " FROM users"
			. " WHERE id IN ("
			. "   SELECT friend_id FROM friends WHERE user_id = ?"
			. " )", $userId)
			->result_array(FALSE);
	}

}
